package com.cskaoyan.connectionpool.opensource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.Properties;

/**
 * 创建日期: 2022/07/01 15:23
 *
 * @author ciggar
 *
 * 获取DBCP数据库连接池
 */
public class DBCPUtils {

    static DataSource dataSource;

    static {

        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream("dbcp.properties"));
            dataSource = BasicDataSourceFactory.createDataSource(properties);

        }catch (Exception ex) {
            System.out.println("创建数据库连接池失败！");
            ex.printStackTrace();
        }
    }

    // 获取连接
    public static Connection getConnection(){

        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;

    }
}
